package com.dev.happy.validation.handle;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import java.util.Set;

/**
 * 参数校验器
 *
 * @param <T> 参数实体定义
 * @author caonuoqi
 */
public class ParamValidateHandle<T> {
    /**
     * 业务中校验参数，如果校验不通过抛出ConstraintViolationException异常统一处理
     *
     * @param param 需要校验的实体
     * @param <T>   实体类型
     */
    public static <T> void validate(T param) {
        Set<ConstraintViolation<T>> errors = Validation.buildDefaultValidatorFactory().getValidator().validate(param);
        if (errors != null && !errors.isEmpty()) {
            throw new ConstraintViolationException("自定义校验", errors);
        }
    }
}
